package cn.dubnation.org.controller;

import cn.dubnation.org.service.IShopService;
import cn.dubnation.org.domain.Shop;
import cn.dubnation.org.query.ShopQuery;
import cn.dubnation.base.util.AjaxResult;
import cn.dubnation.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult insertOrUpdate(@RequestBody Shop shop){
        if( shop.getId()!=null)
            shopService.update(shop);
        else
            shopService.insert(shop);
        return AjaxResult.me();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        shopService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id")Long id) {
        Shop shop = shopService.queryById(id);
        return AjaxResult.me().setData(shop);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult queryAll(){
        List< Shop> list = shopService.queryAll();
        return AjaxResult.me().setData(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody ShopQuery query){
        PageInfo<Shop> pageInfo = shopService.queryPage(query);
        return AjaxResult.me().setData(pageInfo);
    }

    /**
    * 批量删除数据
    * @param ids
    * @return
    */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        shopService.batchDelete(ids);
        return AjaxResult.me();
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid Shop shop) {
        shopService.settlement(shop);
        return AjaxResult.me();
    }

    /**
     * 店铺审核
     * @param shop
     * @return
     */
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody @Valid Shop shop) {
        shopService.audit(shop);
        return AjaxResult.me();
    }

    /**
     * 激活账户
     * @param code
     * @return
     */
    @GetMapping("/active")
    public AjaxResult active(@RequestParam("code") String code) {
        shopService.active(code);
        return AjaxResult.me();
    }
}
